<template>
  <div class="questionInfo" v-if="bol">
    <hmNavBar
      title="刷题"
      path="/home/question"
      right="答题卡"
      @rightClick="rightClick"
    ></hmNavBar>
    <div class="content" v-if="list.length && list[currIndex].detail">
      <div class="title">
        [{{ typeObj[list[currIndex].detail.type] }}]{{
          list[currIndex].detail.title
        }}
      </div>
      <!-- 
          1:报错 
          2:单选多选
          3:ABCD没了
          -->
      <van-tag
        v-for="(item, index) in list[currIndex].detail.tag"
        :key="index"
        class="tag"
        color="#f7f4f5"
        text-color="#c4c4cb"
        >{{ item }}</van-tag
      >
      <!-- 单选 -->
      <ul class="select" v-if="typeObj[list[currIndex].detail.type] === '单选'">
        <!-- 
              正确:有答案且singleAnswer的值与选项索引的值对应上
              错误:有答案且选中的项与选项索引的值对应上且选中的项与singleAnswer的值不相等
           -->
        <li
          class="select-item  iconfont"
          v-for="(item, index) of list[currIndex].detail.option"
          @click="liClick(index)"
          :class="{
            active: ans1 === str[index],
            right:
              list[currIndex].answer &&
              list[currIndex].answer.singleAnswer === str[index],
            error:
              list[currIndex].answer &&
              ans1 === str[index] &&
              list[currIndex].answer.singleAnswer !== ans1
          }"
          :key="index"
        >
          <!-- :class="{active:当前选择的答案要与当前索引值对应上}" -->
          {{ str[index] }}.{{ item }}
        </li>
      </ul>
      <!-- 多选 -->
      <ul
        class="select"
        v-else-if="typeObj[list[currIndex].detail.type] === '多选'"
      >
        <!-- 
            选中:ans2中包含当前选项
            正确:有答案 且 正确答案包含当前选项
           -->
        <li
          class="select-item  iconfont"
          v-for="(item, index) of list[currIndex].detail.option"
          @click="liClick2(index)"
          :class="{
            active: ans2.includes(str[index]),
            right:
              list[currIndex].answer &&
              list[currIndex].answer.multipleAnswer.includes(str[index])
          }"
          :key="index"
        >
          <!-- :class="{active:当前选择的答案要与当前索引值对应上}" -->
          {{ str[index] }}.{{ item }}
        </li>
      </ul>
      <!-- 简答 -->
      <van-field
        v-else
        v-model="ans3"
        type="textarea"
        rows="5"
        class="field"
        @input="inputEvent"
        :disabled="step > 2"
      ></van-field>
      <!-- 答案解析 -->
      <div class="answer" v-if="step > 2">
        <h3 class="answer-title">
          答案解析
        </h3>
        <div class="right-answer">
          <span v-if="typeObj[list[currIndex].detail.type] === '单选'"
            >正确答案:{{ list[currIndex].answer.singleAnswer }}
          </span>
          <span v-else-if="typeObj[list[currIndex].detail.type] === '多选'"
            >正确答案:{{ list[currIndex].answer.multipleAnswer.join(',') }}
          </span>
        </div>
        <div class="other">
          <div class="i1">
            难度:{{ diffObj[list[currIndex].answer.difficulty] }}
          </div>
          <div class="i2">提交次数:{{ list[currIndex].answer.submitNum }}</div>
          <div class="i3">正确次数:{{ list[currIndex].answer.correctNum }}</div>
        </div>
        <div class="txt">
          {{ list[currIndex].answer.answerAnalysis }}
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="b1">
        <div class="iconfont">&#xe611;</div>
        <div class="txt">收藏</div>
      </div>
      <div class="b1">
        <div class="iconfont">&#xe611;</div>
        <div class="txt">反馈</div>
      </div>
      <div class="b2">
        <span>{{ currIndex + 1 }}</span
        >/{{ list.length }}题
      </div>
      <van-button
        class="b3"
        v-if="step < 3"
        :disabled="step === 1"
        @click="submit"
        >提交</van-button
      >
      <van-button class="b3" v-else-if="step === 3" @click="next"
        >下一题</van-button
      >
      <van-button class="b3" v-else>结束</van-button>
    </div>
    <!-- 弹框
    van-popup
        v-model:是否显示弹框
        position:显示位置
     -->
    <van-popup v-model="showCard" position="bottom">
      <!--          
         list进行for循环显示
         正确与错误处理
         点击跳转到相应题
          -->
      <ul class="card">
        <li
          class="card-item"
          @click="skip(index)"
          :class="{
            'card-right': item.answer && item.answer.isRight,
            'card-error': item.answer && !item.answer.isRight
          }"
          v-for="(item, index) in list"
          :key="index"
        >
          {{ index + 1 }}
        </li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
        <li class="noUse"></li>
      </ul>
    </van-popup>
  </div>
</template>
<script>
/*
流程题都先定义一个变量表示当前在做哪一步,写清楚不同的值表示 一个什么过程
1:获取题目,并渲染题目,提交按钮不可点击
2:选择答案,提交按钮可以点击
3:提交后,出现正确答案(正确与错误处理),且出现答案解析,出现下一题
4:提交后,出现正确答案,且出现答案解析,出现结束 (最后一题)
*/
import {
  interviewQuestions,
  questionsSubmit,
  questionsId
} from '@/api/question'
export default {
  data () {
    return {
      showCard: false, // 是否显示答题卡
      bol: true,
      step: 1,
      id: this.$route.query.type,
      city: this.$route.query.city,
      list: [],
      currIndex: 0,
      typeObj: { 1: '单选', 2: '多选', 3: '简答' },
      diffObj: { 1: '简单', 2: '一般', 3: '困难' },
      str: 'ABCDEFG',
      ans1: '', // 单选答案
      ans2: [], // 多选答案,
      ans3: '' // 简答答案
    }
  },
  async created () {
    const res = await interviewQuestions({
      type: this.id,
      city: this.city
    })
    this.list = res.data.data
    window.console.log(res)
  },
  methods: {
    //   单选点击事件
    liClick (index) {
      if (this.step <= 2) {
        this.ans1 = this.str[index]
        this.step = 2
        window.console.log(this.ans1)
      }
    },
    // 多选点击事件
    liClick2 (index) {
      if (this.step <= 2) {
        // 删除
        const _index = this.ans2.indexOf(this.str[index])
        if (_index !== -1) {
          //   数组.splice(起点,删除多少个,新增)
          // ["B","A"]
          this.ans2.splice(_index, 1)
        } else {
          // 新增
          this.ans2.push(this.str[index])
        }

        if (this.ans2.length > 0) {
          //   有选择答案就step=2
          this.step = 2
        } else {
          // 没选择答案
          this.step = 1
        }
        window.console.log(this.ans2)
      }
    },
    // 简答输入
    inputEvent () {
      if (this.ans3.length > 0) {
        this.step = 2
      } else {
        this.step = 1
      }
    },
    // 提交
    async submit () {
      this.$toast.loading({ duration: 0 })
      const res = await questionsSubmit({
        id: this.list[this.currIndex].id,
        singleAnswer: this.ans1,
        multipleAnswer: this.ans2
      })
      this.$toast.clear()
      this.list[this.currIndex].ans1 = this.ans1
      this.list[this.currIndex].ans2 = this.ans2
      this.list[this.currIndex].ans3 = this.ans3
      this.list[this.currIndex].answer = res.data.data
      // currIndex===this.list.length-1
      if (this.currIndex === this.list.length - 1) {
        this.step = 4
      } else {
        this.step = 3
      }

      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
      window.console.log(this.list)
    },
    // 下一题
    async next () {
      this.currIndex++
      this.skip(this.currIndex)
      // 获取题目
      //   this.$toast.loading({ duration: 0 })
      //   const res = await questionsId(this.list[this.currIndex].id)
      //   this.$toast.clear()
      //   this.list[this.currIndex].detail = res.data.data
      //   this.ans1 = ''
      //   this.ans2 = []
      //   this.ans3 = ''
      //   this.step = 1
      //   window.console.log(this.list)
    },
    // k跳转至某一题
    async skip (index) {
      this.currIndex = index
      this.showCard = false
      /*
    如果没有题目
       获取题目 step=1
    如果有题目
        有答案            
             状态处理  step=3/4
             有答案
             显示渲染
        没有答案
              step=1
              状态处理     
    */
      this.ans1 = ''
      this.ans2 = []
      this.ans3 = ''
      if (!this.list[this.currIndex].detail) {
        const res = await questionsId(this.list[this.currIndex].id)
        this.$toast.clear()
        this.list[this.currIndex].detail = res.data.data
        this.step = 1
      } else {
        if (this.list[this.currIndex].answer) {
          if (this.currIndex === this.list.length - 1) {
            this.step = 4
          } else {
            this.step = 3
          }
          this.ans1 = this.list[this.currIndex].ans1
          this.ans2 = this.list[this.currIndex].ans2
          this.ans3 = this.list[this.currIndex].ans3
        } else {
          this.step = 1
        }
      }
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    // 答题卡点击
    rightClick () {
      this.showCard = true
    }
  }
}
</script>
<style lang="less" scoped>
.questionInfo {
  .content {
    padding: 0 @p15;
    .title {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: justify;
      color: #181a39;
      line-height: 22px;
    }
    .tag {
      padding: 5px 8px;
      margin-right: 15px;
    }
    .select {
      margin-top: 30px;
      .select-item {
        padding: 15px;
        border: 1px solid #f7f4f5;
        font-size: 16px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: justify;
        color: #181a39;
        line-height: 22px;
        margin-bottom: 16px;
      }
      .active {
        border: 1px solid #ccc;
      }
      // 对
      .right {
        background-color: #ddfad9;
        color: #45d18c;
        position: relative;
        &::after {
          // &#xe604; 先去掉前三个字符加上\ ,再去掉最后面;
          content: '\e604';
          position: absolute;
          right: 10px;
          top: 50%;
          transform: translateY(-50%);
        }
      }
      // 错误
      .error {
        background-color: #ffefea;
        position: relative;
        &::after {
          // &#xe604; 先去掉前三个字符加上\ ,再去掉最后面;
          content: '\e605';
          position: absolute;
          right: 10px;
          top: 50%;
          color: #ff4949;
          transform: translateY(-50%);
        }
      }
    }
    .field {
      border: 2px solid #ccc;
    }
    // 答案解析
    .answer {
      padding-top: 51px;
      .answer-title {
        opacity: 1;
        font-size: 18px;
        font-family: PingFangSC, PingFangSC-Medium;
        font-weight: 500;
        text-align: left;
        color: #222222;
        line-height: 25px;
        letter-spacing: 0px;
      }
      .right-answer {
        padding: 15px 0;
        font-size: 16px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: justify;
        color: #1dc779;
        line-height: 22px;
      }
      .other {
        background-color: #f7f4f5;
        padding: 10px;
        display: flex;
        font-size: 12px;
        justify-content: space-between;
      }
      .txt {
        font-size: 16px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: justify;
        color: #181a39;
        line-height: 22px;
        padding-bottom: 80px;
        padding-top: 30px;
      }
    }
  }
  .bottom {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 60px;
    padding: 0 @p15;
    display: flex;
    align-items: center;
    text-align: center;
    background-color: #fff;
    .b1 {
      flex: 1;
      color: #ccc;
      .iconfont {
        font-size: 28px;
      }
      .txt {
        opacity: 1;
        font-size: 12px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        color: #b4b4bd;
        line-height: 16px;
        letter-spacing: 0px;
      }
    }
    .b2 {
      flex: 2;
      span {
        opacity: 1;
        font-size: 22px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        color: #e40137;
        line-height: 31px;
        letter-spacing: 1px;
      }
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      color: #545671;
      line-height: 31px;
      letter-spacing: 1px;
    }
    .b3 {
      flex: 2;
      height: 40px;
      background-color: red;
      color: #fff;
    }
    .van-button--normal {
      padding: 0;
    }
  }
  //   答题卡
  .card {
    height: 500px;
    display: flex;
    flex-wrap: wrap;
    padding: 0 @p15;
    justify-content: space-between;
    .card-item {
      width: 33px;
      height: 33px;
      border-radius: 50%;
      font-size: 12px;
      text-align: center;
      line-height: 33px;
      margin: 10px;
      background-color: #ccc;
    }
    .noUse {
      width: 33px;
      height: 0px;
      border-radius: 50%;
      font-size: 12px;
      text-align: center;
      line-height: 33px;
      margin: 10px;
      background-color: #ccc;
    }
    .card-right {
      background-color: #ddfad9;
      color: #84e2ac;
    }
    .card-error {
      background-color: #ffefea;
      color: #ff5a5a;
    }
  }
}
</style>
